﻿using System;
using System.Collections.Generic;
using System.Net;

namespace BaseArith {
    public class BinaryTree<T> : ITree<T> {
        class Node : INode<T> {
            public Node left,right;
            public int growngine;
            public Node(int growngine,T data) {
                this.growngine = growngine;
                this.Data = data;
            }
            //
            public void LeftAdd(Node n) {
                if (this.left == null) {
                    this.left = n;
                    return;
                }
                if (this.left - n >= 0) {
                    this.left.LeftAdd(n);
                } else {
                    this.left.RightAdd(n);
                }
            }
            public void RightAdd(Node n) {
                if (this.right == null) {
                    this.right = n;
                    return;
                }
                if (this.right - n >= 0) {
                    this.right.LeftAdd(n);
                } else {
                    this.right.RightAdd(n);
                }
            }
            public static int operator -(Node n1,Node n2) {
                int b = default(int);
                b = n1.growngine - n2.growngine;
                return b;
            }

            #region INode<T> 成员
            public T Data { get; set; }
            public int Index { get; set; }
            public string Key { get; set; }
            #endregion
        }
        Node root,current; public int Count { get; private set; }
        public BinaryTree() {
            this.root = new BinaryTree<T>.Node(0,default(T));
            this.Count = 0;
        }
        public void Add(int growngine,T data) {
            Node n = new BinaryTree<T>.Node(growngine,data);
            if (this.root - n >= 0) {
                this.root.LeftAdd(n);
            } else {
                this.root.RightAdd(n);
            }
        }
        //
        IEnumerable<T> GetData(Node n) {
            yield return n.Data;
            if (n.left != null)
                foreach (var item in GetData(n.left))
                    yield return item;
            if (n.right != null)
                foreach (var item in GetData(n.right))
                    yield return item;
        }
        public IEnumerable<T> Items {
            get {
                foreach (T item in this.GetData(this.root)) {
                    yield return item;
                }
            }
        }
        //


        #region ITree<T> 成员

        public int GetDepth(INode<T> node) {
            throw new NotImplementedException();
        }

        public INode<T>[] GetChildren(INode<T> pn) {
            throw new NotImplementedException();
        }

        public bool IsLeaf(INode<T> node) {
            throw new NotImplementedException();
        }

        public INode<T> GetParent(INode<T> pc) {
            throw new NotImplementedException();
        }

        public void Add(INode<T> node) {
            throw new NotImplementedException();
        }

        public void Remove(INode<T> node) {
            throw new NotImplementedException();
        }

        public IEnumerable<INode<T>> Query(TreeTraversalType ttt = TreeTraversalType.Pre) {
            throw new NotImplementedException();
        }

        public IEnumerable<INode<T>> Query(INode<T> subnode,TreeTraversalType ttt = TreeTraversalType.Pre) {
            throw new NotImplementedException();
        }

        public int pCount {
            get { throw new NotImplementedException(); }
        }

        public int pDepth {
            get { throw new NotImplementedException(); }
        }

        public double GetWeight(INode<T> node) {
            throw new NotImplementedException();
        }

        #endregion
    }
}


